//
//  NSMutableDictionary+Extension.m
//  learnProject
//
//  Created by 吴斌 on 2022/9/19.
//

#import "NSMutableDictionary+Extension.h"
#import <objc/runtime.h>

@implementation NSMutableDictionary (Extension)

+ (void)load
{
//   类簇:NSString、NSArray、NSDictionay、真实类型是其他类型
    
    Class cls = NSClassFromString(@"__NSDictionaryM");
    Method method1 = class_getInstanceMethod(cls, @selector(setObject:forKeyedSubscript:));
    Method method2 = class_getInstanceMethod(cls, @selector(wb_setObject:forKeyedSubscript:));
    method_exchangeImplementations(method1, method2);
    
    Class cls1 = NSClassFromString(@"__NSDictionaryI");
    Method method3 = class_getInstanceMethod(cls1, @selector(objectForKeyedSubscript:));
    Method method4 = class_getInstanceMethod(cls1, @selector(wb_objectForKeyedSubscript:));
    method_exchangeImplementations(method3, method4);
}

- (void)wb_setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key
{
//    解决key为nil报错问题
    if (!key) return;
    
    [self wb_setObject:obj forKeyedSubscript:key];
}


- (id)wb_objectForKeyedSubscript:(id)key
{
    if (!key) return nil;
    
    return [self wb_objectForKeyedSubscript:key];
}

@end
